#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_WriteDataById_0x2E_test.py
@Time    :   2022/9/14 13:31:28
@Author  :   YangLiang
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from gettext import find
from lib2to3.pgen2 import driver
from pytest_assume.plugin import assume

import pytest
import logging
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True
import Set_up


class Test_DTCSnapshotRecordNUM_phy(object):

   '''NO Fault'''
   ids = ["Request {0}".format(binascii.b2a_hex(bytearray(dtcNumber), ' '), dtcName) for dtcNumber, dtcName in Set_up.DTC_list]
   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in default session with physical addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   @pytest.mark.parametrize('dtcNumber,dtcName', Set_up.DTC_list, ids=ids)
   def test_DTCSnapshotRecordNUM_No_Fault_Default_Phy(self, action, BCMPower_Gear, Driver, dtcNumber,dtcName):
      iStep = 1
      req =  [0x19,0x04] + dtcNumber + [0x01]

      if dtcNumber == [0xAC,0xD1,0x78] or dtcNumber == [0xAC,0xD2,0x78] or dtcNumber == [0xAC,0xE6,0x55]:
         resp = [0x59,0x04] + dtcNumber
      else:
         resp = [0x59,0x04] + dtcNumber + [0x00]
      logging.info(f"Test [{dtcName}:{dtcNumber}] by SID_0x19-Subfunction_0x04-DTCExtendedDataRecordNumb_01")
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False, 'UDS', 'ClearFault')

   @allure.severity('normal')
   @allure.story('Test read dtc information service 0x19')
   @allure.description('Test reportDTCByStatusMask of 0x19 in extended session with physical addressing')
   @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/62a69d6cb35db4f20e37703a/cases')
   @pytest.mark.parametrize('dtcNumber,dtcName', Set_up.DTC_list, ids=ids)
   def test_DTCSnapshotRecordNUM_No_Fault_extended_Phy(self, action, BCMPower_Gear, Driver, dtcNumber,dtcName):
      iStep = 1
      req =  [0x19,0x04] + dtcNumber + [0x01]
      if dtcNumber == [0xAC,0xD1,0x78] or dtcNumber == [0xAC,0xD2,0x78] or dtcNumber == [0xAC,0xE6,0x55]:
         resp = [0x59,0x04] + dtcNumber
      else:
         resp = [0x59,0x04] + dtcNumber + [0x00]
      logging.info(f"Test [{dtcName}:{dtcNumber}]  by SID_0x19-Subfunction_0x04-DTCExtendedDataRecordNumb_01")
      iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False, 'UDS', 'ClearFault')




   @allure.severity('normal')
   @allure.title('Test abnormal voltage DTCSnapshot Record By DTCNumber in default session')
   @allure.description('Test abnormal voltage DTCSnapshot Record By DTCNumber in default session with physical addressing')
   @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
   def test_DTCSnapshotRecordByDTCNumber_01_Default_Phy(self, action, BCMPower_Gear, Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x19,0x04,0xAC,0xCE,0x17,0x01], [0x59,0x04,0xAC,0xCE,0x17,0x00], 1, iStep, False, 'UDS', 'ClearFault')
      self.SnapShot01(Driver, iStep)
      

      
   @allure.severity('normal')
   @allure.title('Test abnormal voltage DTCSnapshot Record By DTCNumber in default session')
   @allure.description('Test abnormal voltage DTCSnapshot Record By DTCNumber in extended session with physical addressing')
   @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
   def test_DTCSnapshotRecordByDTCNumber_01_Extended_Phy(self, action, BCMPower_Gear, Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x19,0x04,0xAC,0xCE,0x17,0x01], [0x59,0x04,0xAC,0xCE,0x17,0x00], 3, iStep, False, 'UDS', 'ClearFault')
      self.SnapShot01(Driver, iStep)


   @allure.severity('normal')
   @allure.title('Test abnormal voltage DTCSnapshot Record By DTCNumber in default session')
   @allure.description('Test abnormal voltage DTCSnapshot Record By DTCNumber in default session with physical addressing')
   @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
   def test_DTCSnapshotRecordByDTCNumber_02_Default_Phy(self, action, BCMPower_Gear, Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x19,0x04,0xAC,0xCE,0x17,0x01], [0x59,0x04,0xAC,0xCE,0x17,0x00], 1, iStep, False, 'UDS', 'ClearFault')
      self.SnapShot02(Driver, iStep)
      

      
   @allure.severity('normal')
   @allure.title('Test abnormal voltage DTCSnapshot Record By DTCNumber in default session')
   @allure.description('Test abnormal voltage DTCSnapshot Record By DTCNumber in extended session with physical addressing')
   @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
   def test_DTCSnapshotRecordByDTCNumber_02_Extended_Phy(self, action, BCMPower_Gear, Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x19,0x04,0xAC,0xCE,0x17,0x01], [0x59,0x04,0xAC,0xCE,0x17,0x00], 3, iStep, False, 'UDS', 'ClearFault')
      self.SnapShot02(Driver, iStep)
      

   @allure.severity('normal')
   @allure.title('Test DTC Snapshot Record data in default session')
   @allure.description('Test DTC Snapshot Record data in in default session with physical addressing')
   @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
   def test_Check_SnapshotRecord_data_Default_Phy(self, action, BCMPower_Gear, Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x19,0x04,0xAC,0xCE,0x17,0x01], [0x59,0x04,0xAC,0xCE,0x17,0x00], 1, iStep, False, 'UDS', 'ClearFault')
      self.SnapShot03(Driver, iStep)

   @allure.severity('normal')
   @allure.title('Test DTC Snapshot Record data in extended session')
   @allure.description('Test DTC Snapshot Record data in in extended session with physical addressing')
   @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
   def test_Check_SnapshotRecord_data_Extended_Phy(self, action, BCMPower_Gear, Driver):
      iStep = 1
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x19,0x04,0xAC,0xCE,0x17,0x01], [0x59,0x04,0xAC,0xCE,0x17,0x00], 3, iStep, False, 'UDS', 'ClearFault')
      self.SnapShot03(Driver, iStep)




   def SnapShot01(self, Driver, iStep):
      # 生成故障
      Driver['CAN'].SetVolt(Driver, 17, 1.5, iStep)

      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x19,0x04,0xAC,0xCE,0x17,0x01], [0x59,0x04,0xAC,0xCE,0x17,0x09,0x01], 0, iStep, False, 'UDS', 'noClearFault')
      snapshot_01 = uds_resp[7:]
      iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)

      # 设置车速>3kph
      Driver['CAN'].write_db_signal('MRR', 'BCM', 'IPB_0x121', 'IPB_Vehicle_Speed_S', 80)

      # 再次生成故障
      Driver['CAN'].SetVolt(Driver, 17, 1.5, iStep)
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x19,0x04,0xAC,0xCE,0x17,0x01], [0x59,0x04,0xAC,0xCE,0x17,0x09,0x01], 0, iStep, False, 'UDS', 'noClearFault')
      
      with allure.step(f"Step{iStep}: Determine if the snapshot is the same as before"):
         with assume: assert uds_resp[7:] == snapshot_01
         iStep += 1

      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x19,0x04,0xAC,0xCE,0x17,0x02], [0x59,0x04,0xAC,0xCE,0x17,0x09,0x02], 0, iStep, False, 'UDS', 'noClearFault')
      snapshot_02 = uds_resp[7:]

      with allure.step(f"Step{iStep}: Determine if the snapshot_02 is not the same as before"):
         with assume: assert  snapshot_02 != snapshot_01
         iStep += 1

      Driver['CAN'].write_db_signal('MRR', 'BCM', 'IPB_0x121', 'IPB_Vehicle_Speed_S', 0)
      # 生成故障
      Driver['CAN'].SetVolt(Driver, 8, 1.5, iStep)

      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x19,0x04,0xAC,0xCF,0x16,0x01], [0x59,0x04,0xAC,0xCF,0x16,0x09,0x01], 0, iStep, False, 'UDS', 'noClearFault')
      snapshot_01 = uds_resp[7:]
      iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)

      # 设置车速>3kph
      Driver['CAN'].write_db_signal('MRR', 'BCM', 'IPB_0x121', 'IPB_Vehicle_Speed_S', 80)

      # 再次生成故障
      Driver['CAN'].SetVolt(Driver, 8, 1.5, iStep)
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x19,0x04,0xAC,0xCF,0x16,0x01], [0x59,0x04,0xAC,0xCF,0x16,0x09,0x01], 0, iStep, False, 'UDS', 'noClearFault')
      
      with allure.step(f"Step{iStep}: Determine if the snapshot is the same as before"):
         with assume: assert uds_resp[7:] == snapshot_01
         iStep += 1

      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x19,0x04,0xAC,0xCF,0x16,0x02], [0x59,0x04,0xAC,0xCF,0x16,0x09,0x02], 0, iStep, False, 'UDS', 'noClearFault')
      snapshot_02 = uds_resp[7:]

      with allure.step(f"Step{iStep}: Determine if the snapshot_02 is not the same as before"):
         with assume: assert  snapshot_02 != snapshot_01
         iStep += 1

      # 设置车速>3kph
      Driver['CAN'].write_db_signal('MRR', 'BCM', 'IPB_0x121', 'IPB_Vehicle_Speed_S', 0)
      Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 0, iStep, False, 'UDS', 'noClearFault')

      return iStep







   def SnapShot02(self, Driver, iStep):
      # 生成故障
      Driver['CAN'].SetVolt(Driver, 17, 1.5, iStep)
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x19,0x04,0xAC,0xCE,0x17,0x02], [0x59,0x04,0xAC,0xCE,0x17,0x09,0x02], 0, iStep, False, 'UDS', 'noClearFault')
      iStep = Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)


      # 设置车速>3kph
      Driver['CAN'].write_db_signal('MRR', 'BCM', 'IPB_0x121', 'IPB_Vehicle_Speed_S', 80)
      Driver['CAN'].read_db_signal('MRR', 'BCM', 'IPB_0x121', 'IPB_Vehicle_Speed_S')

      # 设置电压
      Driver['CAN'].SetVolt(Driver, 15, 1.5, iStep)
      #设置年
      Driver['CAN'].set_signal_Year(22)
      #设置月
      Driver['CAN'].set_signal_Month(9)
      #设置日
      Driver['CAN'].set_signal_Day(16)
      #设置时
      Driver['CAN'].set_signal_Hour(10)
      #设置分
      Driver['CAN'].set_signal_Minute(11)
      #设置秒
      Driver['CAN'].set_signal_Second(12)
      #设置总里程
      Driver['CAN'].set_signal_Total_Distance(1200)
      time.sleep(2)

      # 生成故障
      iStep = Driver['CAN'].DTC_IPB_TIMEOUT_IPB223(iStep, True)
      time.sleep(0.5)

      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x19,0x04,0xC1,0x97,0x80,0x02], [0x59,0x04,0xC1,0x97,0x80,0x09,0x02], 0, iStep, False, 'UDS', 'noClearFault')
      
      with allure.step(f"Step{iStep}: Determine whether the snapshot voltage data is 15v"):
         if uds_resp[10] > 155 or uds_resp[10] < 145:
            actuRespStr = binascii.b2a_hex(bytearray(uds_resp[8:11]), ' ')  
            logging.error(f'correct response:[0x00,0x01,0x96], actual response:[{actuRespStr}]')  
         with assume: assert uds_resp[10] < 155 and uds_resp[10] > 145
         iStep += 1
      
      with allure.step(f"Step{iStep}: Determine whether the snapshot Vehicle speed data is 80"):
         if uds_resp[11:14] != [0x00,0x02,0x04] or uds_resp[14] > 0x8F or uds_resp[14] < 0x89:
            actuRespStr = binascii.b2a_hex(bytearray(uds_resp[11:15]), ' ')  
            logging.error(f'correct response:[0x00,0x02,0x04,0x8B], actual response:[{actuRespStr}]')  
         with assume: assert uds_resp[11:14] == [0x00,0x02,0x04] and uds_resp[14] < 0x8F and uds_resp[14] > 0x89
         iStep += 1
      
      with allure.step(f"Step{iStep}: Determine whether the snapshot Global Real Time: 2022-0916-101112"):
         if uds_resp[18:26] != [0x00,0x04,0x0C,0x0B,0x0A,0x10,0x09,0x16] :
            actuRespStr = binascii.b2a_hex(bytearray(uds_resp[18:26]), ' ')  
            logging.error(f'correct response:[0x00,0x04,0x0C,0x0B,0x0A,0x10,0x09,0x16], actual response:[{actuRespStr}]')  
         with assume: assert uds_resp[18:26] == [0x00,0x04,0x0C,0x0B,0x0A,0x10,0x09,0x16]
         iStep += 1

      # 恢复故障
      iStep = Driver['CAN'].DTC_IPB_TIMEOUT_IPB223(iStep, False)
      Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)
      # 设置车速<3kph
      Driver['CAN'].write_db_signal('MRR', 'BCM', 'IPB_0x121', 'IPB_Vehicle_Speed_S', 0)
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 0, iStep, False, 'UDS', 'noClearFault')

   
   def SnapShot03(self, Driver, iStep):
      # 设置车速>3kph
      Driver['CAN'].write_db_signal('MRR', 'BCM', 'IPB_0x121', 'IPB_Vehicle_Speed_S', 80)

      # 设置电压
      Driver['CAN'].SetVolt(Driver, 15, 1.5, iStep)
      #设置年
      Driver['CAN'].set_signal_Year(22)
      #设置月
      Driver['CAN'].set_signal_Month(9)
      #设置日
      Driver['CAN'].set_signal_Day(16)
      #设置时
      Driver['CAN'].set_signal_Hour(10)
      #设置分
      Driver['CAN'].set_signal_Minute(11)
      #设置秒
      Driver['CAN'].set_signal_Second(12)
      #设置总里程
      Driver['CAN'].set_signal_Total_Distance(1200)
      time.sleep(2)

      # 生成故障
      iStep = Driver['CAN'].DTC_IPB_TIMEOUT_IPB223(iStep, True)
      time.sleep(0.5)

      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x19,0x04,0xC1,0x97,0x80,0x01], [0x59,0x04,0xC1,0x97,0x80,0x09,0x01], 0, iStep, False, 'UDS', 'noClearFault')
      
      with allure.step(f"Step{iStep}: Determine whether the snapshot voltage data is 15v"):
         if uds_resp[10] > 155 or uds_resp[10] < 145:
            actuRespStr = binascii.b2a_hex(bytearray(uds_resp[8:11]), ' ')  
            logging.error(f'correct response:[0x00,0x01,0x96], actual response:[{actuRespStr}]')  
         with assume: assert uds_resp[10] < 155 and uds_resp[10] > 145
         iStep += 1
      
      with allure.step(f"Step{iStep}: Determine whether the snapshot Vehicle speed data is 80"):
         if uds_resp[11:14] != [0x00,0x02,0x04] or uds_resp[14] > 0x8F or uds_resp[14] < 0x89:
            actuRespStr = binascii.b2a_hex(bytearray(uds_resp[11:15]), ' ')  
            logging.error(f'correct response:[0x00,0x02,0x04,0x8B], actual response:[{actuRespStr}]')  
         with assume: assert uds_resp[11:14] == [0x00,0x02,0x04] and uds_resp[14] < 0x8F and uds_resp[14] > 0x89
         iStep += 1
      
      with allure.step(f"Step{iStep}: Determine whether the snapshot Global Real Time: 2022-0916-101112"):
         if uds_resp[18:26] != [0x00,0x04,0x0C,0x0B,0x0A,0x10,0x09,0x16] :
            actuRespStr = binascii.b2a_hex(bytearray(uds_resp[18:26]), ' ')  
            logging.error(f'correct response:[0x00,0x04,0x0C,0x0B,0x0A,0x10,0x09,0x16], actual response:[{actuRespStr}]')  
         with assume: assert uds_resp[18:26] == [0x00,0x04,0x0C,0x0B,0x0A,0x10,0x09,0x16]
         iStep += 1

      with allure.step(f"Step{iStep}: Determine whether the snapshot Odometer: 12000 * 0.1"):
         if uds_resp[18:26] != [0x00,0x04,0x0C,0x0B,0x0A,0x10,0x09,0x16] :
            actuRespStr = binascii.b2a_hex(bytearray(uds_resp[18:26]), ' ')  
            logging.error(f'correct response:[0x00,0x04,0x0C,0x0B,0x0A,0x10,0x09,0x16], actual response:[{actuRespStr}]')  
         with assume: assert uds_resp[26:32] == [0x00,0x05,0x00,0x00,0x2E,0xE0]
         iStep += 1

      # 恢复故障
      iStep = Driver['CAN'].DTC_IPB_TIMEOUT_IPB223(iStep, False)
      Driver['CAN'].SetVolt(Driver, 12, 1.5, iStep)
      # 设置车速<3kph
      Driver['CAN'].write_db_signal('MRR', 'BCM', 'IPB_0x121', 'IPB_Vehicle_Speed_S', 0)
      iStep,uds_resp,size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 0, iStep, False, 'UDS', 'noClearFault')
